import type { DataFormSchema } from '#/components/data-form';

import { ref } from 'vue';

import { $t } from '@croco/framework/locales';

import { service } from '#/api/request';

export interface DictType extends Record<string, any> {
    id?: number;
    name?: string;
    type?: string;
}
export interface DictData extends Record<string, any> {
    /**
     * 字典编码
     */
    id?: number;

    /**
     * 字典排序
     */
    sequence?: number;

    /**
     * 字典标签
     */
    label?: string;

    /**
     * 字典键值
     */
    value?: string;

    /**
     * 字典类型
     */
    type?: string;

    /**
     * 样式
     */
    listClass?: string;

    /**
     * 是否默认
     */
    isDefault?: string;

    /**
     * 备注
     */
    remark?: string;
}

export function useDataTemplate() {
    const dictTypeTemplate: DictType = {
        id: undefined,
        name: undefined,
        type: undefined,
    };
    const dictDataTemplate: DictData = {
        id: undefined,
        label: '未命名',
        value: Date.now().toString(),
        type: '',
        listClass: undefined,
        isDefault: undefined,
        sequence: 0,
        remark: undefined,
    };

    return {
        dictTypeTemplate,
        dictDataTemplate,
    };
}

export function useForm(): DataFormSchema {
    const form = ref<DictType>({});
    const datasource = ref<DictData[]>([]);
    const columns = ref<any[]>([
        { prop: 'label', label: '名称' },
        { prop: 'value', label: '数据值' },
        { prop: 'type', label: '类型' },
        { prop: 'listClass', label: '表格回显样式' },
        { prop: 'isDefault', label: '是否默认值' },
        { prop: 'sequence', label: '序号' },
        { prop: 'remark', label: '备注' },
    ]);
    const rules = reactive({
        name: [{ required: true, message: $t('ui.formRules.required'), trigger: 'blur' }],
        type: [{ required: true, message: $t('ui.formRules.required'), trigger: 'blur' }],
    });
    const onSubmit = (action: string, scope: any): Promise<void> | void => {
        if (action === 'insert') {
            return service.post('/system/dict', { ...scope.row, type: form.value.type }).then(({ data }) => {
                if (data) {
                    Object.assign(scope.row, data);
                }
                data.editing = true;
                datasource.value.push(data);
            });
        } else if (action === 'update') {
            return service.put('/system/dict', scope.row).then(({ data }) => {
                if (data) {
                    Object.assign(scope.row, data);
                }
            });
        } else {
            service.confirm({
                message: `确定对字典数据[${scope.row.label}]进行删除操作?`,
                url: '/system/dict',
                data: { id: [scope.row.id] },
                success: () => {
                    datasource.value = datasource.value.filter((item: any) => item.id !== scope.row.id);
                },
            });
        }
    };
    return {
        form,
        datasource,
        columns,
        rules,
        onSubmit,
    };
}
